#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <errno.h>

#include "objects.h"

#define MAX_SYMBOLS (1 << 14)

scm num_symbols = 0;
char* symboltable[MAX_SYMBOLS];

char* lookup(scm ref) {
	if (ref < num_symbols) {
		return symboltable[ref];
	}
	else {
		fprintf(stderr, "symboltable/lookup: %lu out of bounds %lu\n", ref, num_symbols);
		exit(-1);
	}
}

scm intern(char *name) {
	scm i;

	for (i = 0; i < num_symbols; i++) {
		if (!strcmp(symboltable[i], name)) {
			return mk_sym(i);
		}
	}
  
	symboltable[num_symbols] = malloc(strlen(name)+1);
	if (!symboltable[num_symbols]) {
		fprintf(stderr, "symboltable/intern: malloc failure\n");
	}
  
	strcpy(symboltable[num_symbols], name);
	i = num_symbols++;
  
	if(num_symbols > MAX_SYMBOLS) {
		fprintf(stderr, "Ran out of space for symbols");
		exit(-1);
	}

	return mk_sym(i);
}
